﻿
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace CmdPrep.Algorithms
{    
    static class CmdProcessing
    {
        #region Private static methods

        private static bool tryStartExec(string fileName, string arguments, bool showMessageBox)
        {
            Process p = new Process();
            p.StartInfo.FileName = fileName;
            p.StartInfo.Arguments = arguments;
            
            bool isSuccess;
            
            try 
            {
                p.Start();
                
                isSuccess = true;
            }
            catch (System.Exception) 
            {                
                isSuccess = false;
            }
            
            if (showMessageBox && ! isSuccess)
            {
                MessageBox.Show(
                    "Failed to start '" + fileName + "'", 
                    "CmdPrep - Executable not found",
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Information);
            }

            return isSuccess;
        }
        
        #endregion
        
        #region Public static methods
        
        public static bool processCmdFile(string inputFullName, string outputFullName, bool keepEqualSign, bool skipEmptyLines)
        {
            StreamReader reader = new StreamReader(inputFullName);
            StreamWriter writer = new StreamWriter(outputFullName, false);
            Replacement editing = new Replacement();
    
            string line = null;
            while ((line = reader.ReadLine()) != null) 
            {
                line = editing.remove_right(line, @"\", " ") ;          // remove whitespace after \
                line = editing.remove_right(line, "\"", " ") ;          // remove whitespace after "
                line = editing.remove_left(line, "\"", " ") ;          // remove whitespace after "
                if (!keepEqualSign)
                    line = editing.remove_right(line, "\"", "=") ;      // remove = after \ (i.e. in front of "Doc.type")
                line = editing.remove_left(line, "-", " ") ;            // remove whitespace -
                line = editing.remove_left(line, ":", " ") ;            // remove whitespace :
                line = editing.remove_right(line, " ", "-") ;            // remove bfore -
                line = editing.remove_left(line, @"\", " ") ;           // remove whitespace before \
                line = editing.remove_left(line, "*", " ") ;            // remove whitespace before * (as in *.*)
                line = editing.remove_left(line, ".", " ") ;            // remove whitespace before .
                line = editing.remove_left(line, "_", " ") ;            // remove whitespace before _
                line = editing.add_left(line, "/", " ", " ") ;          // add whitespace after / (/ usually preceedes a parameter)
                line = editing.replace(line, "^", " ") ;                // replace ^ with whitespace
                line = editing.remove_end(line, " ") ;                    // remove trailing whitespces!
                
                if (!((string.IsNullOrEmpty(line)) &&  skipEmptyLines))
                    writer.WriteLine(line);
            }
            
            writer.Close();
            reader.Close();
            
/*            MessageBox.Show("File '" + Path.GetFileName(outputFullName) + "' processed.",
                            "CmdPrep - Processing done", 
                            MessageBoxButtons.OK, 
                            MessageBoxIcon.Information);
*/
            //CmdPrep.Frontends.CmdControl.statusChanged("CMD file Processed") .... //something, but not callable ???  ;

            return true;
        }

        public static bool editCmdFile(string fullName)
        {
            return
                tryStartExec( "psedit.exe",    fullName, false) ||
                tryStartExec( "notepad++.exe",    fullName, false) ||
                tryStartExec( "notepad.exe",    fullName, true);
        }

        public static bool diffCmdFiles(string fullName1, string fullName2)
        {
            string arguments = "\"" + fullName1 + "\" \"" + fullName2 + "\"";
            
            return
                tryStartExec( "psedit.exe",    arguments, false) ||
                tryStartExec( "winmerge.exe",    arguments, true);
        }
        
        #endregion
    }
}